//
//  WCAListOperateViewController.m
//  WeChatBuniess
//
//  Created by Guo Rui on 2017/12/26.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "WCAListOperateViewController.h"
#import "TFDataCaptureManager.h"
#import "WCAAutoMakeBookManager.h"
#import "TFToast.h"

@interface WCAListOperateViewController ()
@property (strong, nonatomic) UILabel *titleLabel;

@property (strong, nonatomic) UILabel *reciveCountLabel;
@property (strong, nonatomic) UILabel *sendCountLabel;
@property (strong, nonatomic) UILabel *faileCountLabel;
@end

@implementation WCAListOperateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = [NSString stringWithFormat:@"正在制作%@的微信书", self.contact.m_nsNickName];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self layoutViews];
    
    self.titleLabel.text = @"进行中...";
    __weak WCAListOperateViewController *_self = self;
    
    TFDataCaptureManager.sharedInstance.reciveCountBlock = ^(NSInteger count) {
        [_self updateReciveCount:count];
    };
    
    TFDataCaptureManager.sharedInstance.reciveDataCompletion = ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            _self.reciveCountLabel.text = @"抓取数据完成";
        });
    };
    
    TFDataCaptureManager.sharedInstance.sendFailCountBlock = ^(NSInteger count) {
        [_self updateFailCount:count];
    };
    
    TFDataCaptureManager.sharedInstance.sendSuccessCountBlock = ^(NSInteger count) {
        [_self updateSendCount:count];
    };
    
    TFDataCaptureManager.sharedInstance.sendDataCompletion = ^(BOOL hasFail, NSArray <NSDictionary *>*failArray) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (hasFail == NO) {
                _self.titleLabel.text = @"制作成功";
            }
            else {
                _self.titleLabel.text = @"制作失败，已存入失败列表！";
            }
            
            [_self dismissViewControllerAnimated:YES completion:^{
                
                __strong WCAListOperateViewController *self = _self;
                if (self.completion) {
                    self.completion();
                }
                
                NSDictionary *dict = nil;
                if (hasFail) {
                    dict = @{ @"contact" : self.contact, @"success" : @(NO), @"failArray" : failArray};
                }
                else {
                    dict = @{ @"contact" : self.contact, @"success" : @(YES) };
                }
                
                [[NSNotificationCenter defaultCenter] postNotificationName:WCAAutoMakeBookCompletionNotification object:dict];
                
            }];
        });
    };
    
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
//        __weak WCAListOperateViewController *_self = self;
//        [self dismissViewControllerAnimated:YES completion:^{
//            __strong WCAListOperateViewController *self = _self;
//            if (self.completion) {
//                self.completion();
//            }
//
//            NSString *path = [[NSBundle mainBundle] pathForResource:@"test.plist" ofType:nil];
//            NSArray *mokeFileArray = [NSArray arrayWithContentsOfFile:path];
//            [[NSNotificationCenter defaultCenter] postNotificationName:WCAAutoMakeBookCompletionNotification object:@{ @"contact" : self.contact, @"success" : @(YES), @"failArray" : mokeFileArray}];
//
//        }];
//    });
}

- (void)layoutViews
{
    CGFloat x, y, width, height;
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    
    {
        // name
        width = 300.f;
        height = 35.f;
        x = (screenWidth - width) / 2.f;
        y = 350.f;
        self.titleLabel.frame = CGRectMake(x, y, width, height);
    }
    
    {
        // recivecount
        x = 40.f;
        height = 40.f;
        y = (screenHeight - height) / 2.f - 30.f;
        width = screenWidth - 2 * x;
        self.reciveCountLabel.frame = CGRectMake(x, y, width, height);
    }
    
    {
        // sendcount
        x = 40.f;
        height = 40.f;
        y = CGRectGetMaxY(self.reciveCountLabel.frame) + 30.f;
        width = screenWidth - 2 * x;
        self.sendCountLabel.frame = CGRectMake(x, y, width, height);
    }
    
    {
        // failecount
        x = 40.f;
        height = 40.f;
        y = CGRectGetMaxY(self.sendCountLabel.frame) + 30.f;
        width = screenWidth - 2 * x;
        self.faileCountLabel.frame = CGRectMake(x, y, width, height);
    }
}

- (void)updateTitle:(NSString *)tile
{
    dispatch_async(dispatch_get_main_queue(), ^{
        self.titleLabel.text = tile;
    });
}

- (void)updateReciveCount:(NSInteger)count
{
    dispatch_async(dispatch_get_main_queue(), ^{
        self.reciveCountLabel.text = [NSString stringWithFormat:@"已抓取 : %zd条数据", count];
    });
}

- (void)updateSendCount:(NSInteger)count
{
    dispatch_async(dispatch_get_main_queue(), ^{
        self.sendCountLabel.text = [NSString stringWithFormat:@"已发送 : %zd条数据", count];
    });
}

- (void)updateFailCount:(NSInteger)count
{
    dispatch_async(dispatch_get_main_queue(), ^{
        self.faileCountLabel.text = [NSString stringWithFormat:@"发送失败 : %zd条数据", count];
    });
}

#pragma mark - Getter
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:26];
        [self.view addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UILabel *)reciveCountLabel {
    if (!_reciveCountLabel) {
        _reciveCountLabel = [[UILabel alloc] init];
        _reciveCountLabel.textColor = [UIColor blackColor];
        _reciveCountLabel.textAlignment = NSTextAlignmentCenter;
        _reciveCountLabel.font = [UIFont systemFontOfSize:23];
        _reciveCountLabel.text = @"已抓取 : 0条数据";
        [self.view addSubview:_reciveCountLabel];
    }
    return _reciveCountLabel;
}

- (UILabel *)sendCountLabel {
    if (!_sendCountLabel) {
        _sendCountLabel = [[UILabel alloc] init];
        _sendCountLabel.textColor = [UIColor greenColor];
        _sendCountLabel.textAlignment = NSTextAlignmentCenter;
        _sendCountLabel.font = [UIFont systemFontOfSize:23];
        _sendCountLabel.text = @"已发送 : 0条数据";
        [self.view addSubview:_sendCountLabel];
    }
    return _sendCountLabel;
}

- (UILabel *)faileCountLabel {
    if (!_faileCountLabel) {
        _faileCountLabel = [[UILabel alloc] init];
        _faileCountLabel.textColor = [UIColor redColor];
        _faileCountLabel.textAlignment = NSTextAlignmentCenter;
        _faileCountLabel.font = [UIFont systemFontOfSize:23];
        _faileCountLabel.text = @"发送失败 : 0条数据";
        [self.view addSubview:_faileCountLabel];
    }
    return _faileCountLabel;
}

- (void)dealloc {
    [TFDataCaptureManager.sharedInstance clean];
    NSLog(@"\n************\n %@ dealloc \n************", self.class);
}
@end
